<template>
  <a-modal
    :open="props.modelVisible"
    title="课程管理"
    :destroyOnClose="true"
    @cancel="handleCancel"
    @ok="handleOk"
    width="710px"
  >
    <div class="container">
      <div style="width: 45%">
        <a-input-group compact style="margin-top: 10px">
          <a-select style="width: 30%" v-model:value="selectType">
            <a-select-option value="名称">名称</a-select-option>
            <a-select-option value="课程号">课程号</a-select-option>
          </a-select>
          <a-input-search
            placeholder="搜索"
            style="width: 70%"
            v-model:value="selectInput"
            @search="findCourse"
            enter-button
          />
        </a-input-group>
        <a-divider></a-divider>
        <!-- <a-tabs v-model:activeKey="selectGroup" :tabBarStyle="{ padding: '0 2%', margin: '0' }">
                    <a-tab-pane :key=1 tab=""></a-tab-pane>
                    <a-tab-pane :key=2 tab=""></a-tab-pane>
                </a-tabs> -->

        <a-list style="height: 73%; overflow-y: auto; margin-top: 10px" :data-source="unSelectData">
          <a-spin :spinning="spinning" v-if="spinning" style="margin: 40% 0" />
          <template #renderItem="{ item }">
            <a-list-item class="item" @click="selectItem(item)" :key="item.courseId">
              <span style="width: 70%"> {{ item.courseName }}</span>
              <template #actions>
                <CheckOutlined
                  style="color: rgb(102 152 255)"
                  v-if="selectedDate.some((obj) => obj.courseId === item.courseId)"
                />
              </template>
            </a-list-item>
          </template>
        </a-list>
      </div>
      <a-divider type="vertical" style="height: 100%" />
      <div style="width: 45%">
        <div style="margin-top: 10px">已选择 · {{ selectedDate.length }}</div>
        <a-list style="height: 85%; overflow-y: auto; margin-top: 10px" :data-source="selectedDate">
          <template #renderItem="{ item }">
            <a-list-item class="item">
              <span> {{ item.courseName }}</span>
              <template #actions>
                <CloseOutlined class="deleteIcon" @click="deleteItem(item.courseId)" />
              </template>
            </a-list-item>
          </template>
        </a-list>
      </div>
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
import UserDao from '@/daos/user_dao';
import CourseDao from '@/daos/course_dao';
import ProjecrDao from '@/daos/project_dao';
import { message } from 'ant-design-vue';
import { useStore } from 'vuex';
const projectDao = new ProjecrDao();
const store = useStore();
const courseDao = new CourseDao();
const userDao = new UserDao();

interface Course {
  courseId: number;
  couserName: string;
}
const props = defineProps({
  modelVisible: {
    type: Boolean,
    default: false
  },
  courseList: {
    type: Array as () => Course[]
  }
});
const emit = defineEmits(['update:modelVisible', 'confirm']);
//筛选
const selectType = ref('名称');
const selectGroup = ref(1);
const selectInput = ref();
//加载开关
const spinning = ref<boolean>(true);
//带选择人员数组
let unSelectData = ref([]);
const selectedDate = ref([]);
onMounted(() => {
  findCourse();
  if (props.courseList?.length > 0) {
    selectedDate.value = props.courseList.map((item) => ({ ...item }));
  }
});

const findCourse = async () => {
  spinning.value = true;
  if (selectType.value === '课程号') {
    const result = await courseDao.findCourse(selectInput.value, null);
    console.log(result);

    unSelectData.value = result.data.data;
    spinning.value = false;
  } else if (selectType.value === '名称') {
    const result = await courseDao.findCourse(null, selectInput.value);
    unSelectData.value = result.data.data;
    spinning.value = false;
  }
};

const selectItem = (newItem) => {
  let index = selectedDate.value.findIndex((item) => item.courseId === newItem.courseId);
  if (index === -1) {
    selectedDate.value.push(newItem);
  } else {
    selectedDate.value.splice(index, 1);
  }
};

const deleteItem = (courseId) => {
  let index = selectedDate.value.findIndex((item) => item.courseId === courseId);
  selectedDate.value.splice(index, 1);
};
const handleCancel = () => {
  emit('update:modelVisible', false);
};
const handleOk = async () => {
  const result = await projectDao.upsertDeleteCourses(store.getters.projectId, selectedDate.value);
  message.success(result.data.data);
  emit('confirm');
  emit('update:modelVisible', false);
};
watch(
  () => props.modelVisible,
  (newValue) => {
    unSelectData.value = [];
    findCourse();
    if (newValue === true && props.courseList?.length > 0) {
      selectedDate.value = props.courseList.map((item) => ({ ...item }));
    }
  }
);
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  justify-content: space-around;
  width: 100%;
  height: 440px;
}

.item {
  display: flex;
  justify-content: space-between;
}

.item:hover {
  cursor: pointer;
  background-color: #f5f5f5aa;
}

.deleteIcon:hover {
  color: red;
}
</style>
